<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<script type="text/javascript">
	$(function() {
		parent.$.messager.progress('close');

		var residenceId = '${residenceId}';
		var parentId = '${parentId}';
		var buildingId = '${buildingId}';
		$.ajaxSetup({
			  async: false
		 });
		
		var unitBox = $('#building-unitid').combobox({
			onLoadSuccess : function(obj){
				if(buildingId && buildingId != ''){
					$('#building-unitid').combobox('setValue',buildingId);
					return ;
				}
				try{
					var id = obj[0].id;
					$('#building-unitid').combobox('setValue',id);
				} catch(e){}
			}
		});
		
		var buildingBox = $('#parent-id').combobox({
				onSelect : function(a){
				unitBox.combobox('setValue','');
				unitBox.combobox('reload','/buildingUnitManager/combobox?parentId=' + $('#parent-id').combobox('getValue'));
			},
			onLoadSuccess : function(a){
				try{
					$('#parent-id').combobox('setValue',parentId);
					if(parentId && parentId != null && parentId != ''){
						unitBox.combobox('reload','/buildingUnitManager/combobox?parentId=' + parentId);
					} else {
						var pid = a[0].id;
						if(pid && pid != ''){
							$('#parent-id').combobox('setValue',pid);
							unitBox.combobox('reload','/buildingUnitManager/combobox?parentId=' + pid);
						}
					}
				} catch(e){}
			}
		});
		
		var typeBox = $('#building-typeId').combobox({url:'/buildingType/combobox'});
		
		$('#residenceId').combobox({
			onSelect : function(a){
				unitBox.combobox('setValue','');
				$('#building-unitid').combobox('loadData',[]);
				$('#building-unitid').combobox('setValue','');
				parentId = '';
				buildingId = '';
				buildingBox.combobox('reload','/buildingManager/combobox?residenceId=' + $('#residenceId').combobox('getValue')	);
				typeBox.combobox('reload','/buildingType/combobox?residenceId=' + $('#residenceId').combobox('getValue'));
			},
			onLoadSuccess : function(){
				$('#residenceId').combobox('setValue',residenceId);
				if(residenceId && residenceId != ''){
						$('#parent-id').combobox('reload','/buildingManager/combobox?residenceId=' + residenceId);
						typeBox.combobox('reload','/buildingType/combobox?residenceId=' + residenceId);
				}
			}
		});
		
		
		if(buildingId && buildingId != ''){
		//	unitBox.combobox('reload','/buildingUnitManager/combobox?parentId=' +parentId);
		}
		
		$('#form').form({
			url : '/houseDoorplatManager/add',
			onSubmit : function() {
				parent.$.messager.progress({
					title : '提示',
					text : '数据处理中，请稍后....'
				});
				var isValid = $(this).form('validate');
				if (!isValid) {
					parent.$.messager.progress('close');
				}
				return isValid;
			},
			success : function(result) {
				parent.$.messager.progress('close');
				try{
					result = $.parseJSON(result);
					if (result.success) {
						parent.$.modalDialog.openner_datagrid.datagrid('reload');
						parent.$.modalDialog.handler.dialog('close');
					}  else {
						parent.$.messager.alert("提示",result.msg,"info");
					}
				} catch(e){
					parent.$.messager.alert("提示",result,"info");
				}
			}
		});
	});
</script>
<div class="easyui-layout" data-options="fit:true,border:false">
	<div data-options="region:'center',border:false" title="" style="overflow-y:auto;">
		<form id="form" method="post">
			<table class="table table-hover table-condensed">
			<c:choose>
				<c:when test="${isAdmin}">
					<tr>
						<th>选择小区</th>
						<td>
							<select  name="residenceId"  id="residenceId" type="text" placeholder="小区名字"  class=""  data-options="required:true,editable:false,width:380,icons:[{
											iconCls:'icon-clear',
											handler: function(e){
												$(e.data.target).combobox('setValue','');
												$('#parent-id').combobox('loadData',[]);
												$('#building-unitid').combobox('loadData',[]);
												$('#parent-id').combobox('clear');
												$('#building-unitid').combobox('clear');
											}
										}]" >
									<c:forEach items="${residentces }" var="r">
												<option value="${r.id }">${r.name}</option>
									</c:forEach>
							</select>
						</td>
					</tr>
				</c:when>
				<c:otherwise>
				<tr>
					<th>当前小区</th>
					<td>
					<input name="residenceId" type="hidden" class="span2" value="${residence.id }" readonly="readonly"/>
					<input name="residenceName"  readonly="readonly" value="${residence.name }" />
					</td>
				</tr>
				</c:otherwise>
			</c:choose>
				<tr>
					<th>楼栋</th>
					<td>
							<select name="parentId" id="parent-id" class="" data-options="required:true,width:380,editable:false,icons:[{
											iconCls:'icon-clear',
											handler: function(e){
												$(e.data.target).combobox('setValue','');
												$('#building-unitid').combobox('loadData',[]);
												$('#building-unitid').combobox('clear');
											}
										}]" >
								<c:forEach items="${building }" var="b">
										<option value="${b.id }">${b.name}</option>
								</c:forEach>		
										
							</select>
					</td>
				</tr>
				<tr>
					<th>单元名称</th>
					<td>
					<input name="buildingId" id="building-unitid" type="text" placeholder="单元名称" class="" data-options="required:true,editable:false,width:380,icons:[{
											iconCls:'icon-clear',
											handler: function(e){
												$(e.data.target).combobox('setValue','');
											}
										}]" >
					</td>
				</tr>
				<tr>
					<th>楼栋类型</th>
					<td>
					<input name="typeId" id="building-typeId" type="text" placeholder="楼栋类型" class="" data-options="required:true,editable:false,width:380,icons:[{
											iconCls:'icon-clear',
											handler: function(e){
												$(e.data.target).combobox('setValue','');
											}
										}]" >
					</td>
				</tr>
				<tr>
					<th>门牌号</th>
					<td>
					<input name="num" type="text" placeholder="门牌号" style="width:380px"  class="easyui-validatebox span2" data-options="required:true" value="">
					</td>
				</tr>
				<tr>
					<th>业主名称</th>
					<td>
					<input name="ownerName" style="width:380px" type="text" placeholder="业主名称" class="easyui-validatebox span2" data-options="required:true" value="">
					</td>
				</tr>
				<tr>
					<th>业主联系方式</th>
					<td>
					<input name="ownerMobile" style="width:380px" type="text" placeholder="业主联系方式" class="easyui-validatebox span2" data-options=""  value="">
					</td>
				</tr>
				<tr>
					<th>入住时间</th>
					<td>
					<input name="useDate" type="text" style="width:380px" placeholder="业主联系方式" class="easyui-datebox span2" data-options="required:true,editable:false"  value="">
					</td>
				</tr>
				<tr>
					<th>建筑面积(平方米)</th>
					<td>
					<input name="space" type="text" style="width:380px" placeholder="建筑面积(平方米)" class="easyui-numberbox" data-options="precision:2,required:true" >
					</td>
				</tr>
				<tr>
					<th>状态</th>
					<td>
					<input name="status" type="text" placeholder="状态" class="easyui-combobox"   value="1" data-options="
								editable:false,
								required:true,
								panelHeight:'auto',
								data : [{value:1,text:'自住'},{value:2,text:'出租'},{value:3,text:'其他'}]
								">
					</td>
				</tr>
				<tr>
					<th>描述</th>
					<td>
					<textarea name="comment" style="width:380px"></textarea>
					</td>
				</tr>
			</table>
		</form>
	</div>
</div>